package com.demo.androidgame.samgukji_hit;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.demo.androidgame.fx.Game;
import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.impl.GLScreen;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Rectangle;
import com.demo.androidgame.fx.math.Vector2;

public class ActScreen extends GLScreen{

	Camera2D guiCam;
	SpriteBatcher batcher;
	
	Rectangle actOne;
	Rectangle actTwo;
	Rectangle actThree;
	Rectangle actFour;
	Rectangle actFive;
	Rectangle actSix;
	
	Vector2 touchPoint;
	
	
	
	public ActScreen(Game game) {
		super(game);
		guiCam = new Camera2D(glGraphics, 800, 480);
		batcher = new SpriteBatcher(glGraphics, 100);
		
		actOne = new Rectangle(55, 290, 190, 120);
		actTwo = new Rectangle(310, 290, 190, 120);
		actThree = new Rectangle(560, 290, 190, 120);
		actFour = new Rectangle(55, 70, 190, 120);
		actFive = new Rectangle(310, 70, 190, 120);
		actSix = new Rectangle(560, 70, 190, 120);
		
		touchPoint = new Vector2();
	}

	@Override
	public void update(float deltaTime) {
		
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);                        
            if(event.type == TouchEvent.TOUCH_UP) {
                touchPoint.set(event.x, event.y);
                guiCam.touchToWorld(touchPoint);
                
                if (OverlapTester.pointInRectangle(actOne, touchPoint)) {
                	if (Assets.actNumber < 2) {
                		Assets.actNumber = 2;
                	}
                	game.setScreen(new ActOneScreen(game));
            		return;
                }
                if(Assets.clearHeroStage >= 121 && Assets.clearMusicStage >= 221){
	                if (OverlapTester.pointInRectangle(actTwo, touchPoint)) {
	                	if (Assets.actNumber < 3) {
	                		Assets.actNumber = 3;
	                	}
	                	game.setScreen(new ActTwoScreen(game));
                		return;
	                }
                }
                if(Assets.clearHeroStage >= 131 && Assets.clearMusicStage >= 231){
	                if (OverlapTester.pointInRectangle(actThree, touchPoint)) {
	                	if (Assets.actNumber < 4) {
	                		Assets.actNumber = 4;
	                	}
                	game.setScreen(new ActTwoScreen(game));
            		return;
	                }
                }
                if(Assets.clearHeroStage >= 141 && Assets.clearMusicStage >= 241){
	                if (OverlapTester.pointInRectangle(actFour, touchPoint)) {
	                	if (Assets.actNumber < 5) {
	                		Assets.actNumber = 5;
	                	}
	                	game.setScreen(new ActTwoScreen(game));
                		return;
	                }
                }
               if(Assets.clearHeroStage >= 151 && Assets.clearMusicStage >= 251){
	                if (OverlapTester.pointInRectangle(actFive, touchPoint)) {
	                	if (Assets.actNumber < 6) {
	                		Assets.actNumber = 6;
	                	}
                		game.setScreen(new ActTwoScreen(game));
                		return;
	                }
                }
               if(Assets.clearHeroStage >= 161 && Assets.clearMusicStage >= 261){
	                if (OverlapTester.pointInRectangle(actSix, touchPoint)) {
	                	if (Assets.actNumber < 7) {
	                		Assets.actNumber = 7;
	                	}
                		game.setScreen(new ActTwoScreen(game));
                		return;
	                }
                }
            }
        }
		
	}

	@Override
	public void present(float deltaTime) {
		GL10 gl = glGraphics.getGL();        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();
        gl.glEnable(GL10.GL_TEXTURE_2D);
        
        batcher.beginBatch(Assets.mActSelectMenu);
        batcher.drawSprite(400, 240, 800, 480, Assets.mActSelectMenuRegion);
        batcher.endBatch();


        gl.glEnable(GL10.GL_BLEND);
        gl.glDisable(GL10.GL_TEXTURE_2D);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 
		
	}

	@Override
	public void pause() {
		
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

}
